package com.tiheima.controller;

import com.tiheima.model.dos.EmpListDO;
import com.tiheima.model.dto.emp.AddEmpDTO;
import com.tiheima.model.dto.emp.EmpAndExprListDTO;
import com.tiheima.model.dto.emp.EmpPageQuery;
import com.tiheima.model.result.PageResult;
import com.tiheima.model.result.Result;
import com.tiheima.model.vo.EmpAndExprListByIdVO;
import com.tiheima.model.vo.GetEmpListVO;
import com.tiheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    private final EmpService empService;

    public EmpController(EmpService empService) {
        this.empService = empService;
    }

    @GetMapping
    public Result<PageResult<EmpListDO>> page(EmpPageQuery query) {
        PageResult<EmpListDO> pageResult = empService.getPage(query);
        return Result.success(pageResult);
    }

    @DeleteMapping
    public Result<?> deleteEmp(@RequestParam("ids") ArrayList<Integer> ids) throws Exception {
        empService.deleteEmp(ids);
        return Result.success();
    }

    @PostMapping
    public Result<?> addEmp(@RequestBody AddEmpDTO dto){
        log.info("新增员工信息：{}",dto);
        empService.addEmp(dto);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<EmpAndExprListByIdVO> getEmpById(@PathVariable("id") Integer id){
        log.info("根据id查询员工信息：{}",id);
        return Result.success(empService.getEmpAndExprListById(id));
    }

    @PutMapping
    public Result<?> updateEmp(@RequestBody EmpAndExprListDTO empAndExprListDTO){
        log.info("修改员工信息：{}",empAndExprListDTO);
        empService.updateEmpAndExpr(empAndExprListDTO);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<GetEmpListVO>> getEmpList(){
        List<GetEmpListVO> vo = empService.getEmpList();
        return Result.success(vo);
    }
}
